package com.scnu.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scnu.edu.entity.User;
import com.scnu.edu.mapper.UserMapper;
import com.scnu.edu.service.OrderService;
import com.scnu.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = this.getOne(queryWrapper);

        if (user != null && user.getPassword().equals(password)) {
            // 清除密码（安全考虑）
            user.setPassword(null);
            // 登录时检查VIP状态
            return checkAndUpdateVipStatus(user);
        }
        return null;
    }


    @Override
    public boolean register(User user) {
        // 检查用户名和邮箱是否已存在
        if (checkUsername(user.getUsername()) || checkEmail(user.getEmail())) {
            return false;
        }

        // 设置默认值
        user.setIsVip(false);
        user.setCreateTime(new Date());

        return this.save(user);
    }

    @Override
    public boolean checkUsername(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        return this.count(wrapper) > 0;
    }

    @Override
    public boolean checkEmail(String email) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, email);
        return this.count(wrapper) > 0;
    }

    @Override
    public User updateVipStatus(Long userId, Integer vipDays) {
        User user = this.getById(userId);
        if (user != null) {
            Date now = new Date();
            Date expireTime = user.getVipExpireTime();

            if (expireTime == null || expireTime.before(now)) {
                expireTime = now;
            }

            long addMillis = (long) vipDays * 24 * 60 * 60 * 1000;
            Date newExpireTime = new Date(expireTime.getTime() + addMillis);

            user.setVipExpireTime(newExpireTime);
            user.setIsVip(true);
            this.updateById(user);

            return user;
        }
        return null;
    }

    @Override
    public User checkAndUpdateVipStatus(User user) {
        if (user.getIsVip() != null && user.getIsVip()) {
            Date now = new Date();
            if (user.getVipExpireTime() != null && user.getVipExpireTime().before(now)) {
                // VIP已过期，更新状态
                user.setIsVip(false);
                this.updateById(user);
            }
        }
        return user;
    }

}